package com.ysd.twocrm.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysd.twocrm.entity.ClassCourse;
import com.ysd.twocrm.entity.Course;
import com.ysd.twocrm.entity.CourseChildren;
import com.ysd.twocrm.entity.Video;
import com.ysd.twocrm.mapper.ClassCourseMapper;
import com.ysd.twocrm.mapper.CourseMapper;
import com.ysd.twocrm.mapper.VideoMapper;
import com.ysd.twocrm.service.CourseChildrenService;
import com.ysd.twocrm.utils.LayuiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/coursechildren")
public class CourseChildrenController {
    @Autowired
    private CourseChildrenService courseChildrenService;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private ClassCourseMapper classCourseMapper;

    /*
     * @Description 多条件查询章节表，
     * @param page 页码
     * @param csc 根据课程id来查询
     * @return java.lang.Object  返回layui格式的工具类
     * @author liusupeng
     * @date 2020/4/30 8:24
     */
    @GetMapping
    public Object getAllLayuiCourseChildren(Integer page, Integer cid,Integer userId){
        List<Integer> cidstr=new ArrayList<>();
               //  List<Integer> cidstr=new ArrayList<>();
                     if(cid==null){
                 List<Course> allCourse = courseMapper.findAllCourse(userId);
                         if(allCourse.isEmpty()){
                             cidstr.add(0);
                         }else{
                             for(int i=0;i<allCourse.size();i++){
                                 cidstr.add(allCourse.get(i).getCid());
                             }
                         }

                     }else{
                         cidstr.add(cid);
                     }



        return courseChildrenService.COURSE_CHILDREN_LAYUI_UTILS(page,cidstr);
    }
    /*
     * @Description 查询全部学期，需要去重
     * 这里通过for循环，首先循环遍历课程然后创建一个集合用来存，然后在里面进行判断当前集合内是否有和他相似的名字如果有则不管他继续循环
     * 如果不相似的话就添加进去。这里一定要用list集合，这样的话有序，如果用set等无序的来做和想要的效果会不一样
     * @param
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/5/9 16:12
     */
    @GetMapping("/1")
    public Object getAllCourseBySemester(Integer userId){
        List<Course> courses = courseMapper.findAllCourse(userId);
        List<String>slist=new ArrayList<>();
        for(int i=0;i<courses.size();i++){
            if(!slist.contains(courses.get(i).getcSemester())){
               slist.add(courses.get(i).getcSemester());
           }
        }
        return slist;
    }
    /*
     * @Description 根据学期来查询的当前学期下的课程信息
     * @param cSemester
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/5/9 17:24
     */
@GetMapping("/2")
    public Object getAllBySemesterFindcName(String cSemester,Integer userId){

        List<Course> courses = courseMapper.findAllCourseByCsemester(userId,cSemester);
        return courses;
    }
    /*
     * @Description 根据课程姓名来查询当前的id 用于表格重载
     * @param cname
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/5/9 17:26
     */
    @GetMapping("/3")
    public Object getAllBycNameFindByCid(String cname){

        QueryWrapper<Course> courseQueryWrapper=new QueryWrapper<Course>();
        courseQueryWrapper.eq("cname",cname);
        Course courses = courseMapper.selectOne(courseQueryWrapper);
        return courses.getCid();
    }
    /*
     * @Description 根据章节id来查询视频表
     * @param scid
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/5/10 10:30
     */
@GetMapping("/4")
    public Object getAllVideoByCid(Integer scid){

         QueryWrapper<Video> queryWrapper=new QueryWrapper<Video>();
             queryWrapper.eq("scid",scid);
    LayuiUtils<Video> layuiUtils=new LayuiUtils<Video>();
    layuiUtils.setData(videoMapper.selectList(queryWrapper));
        return layuiUtils;
    }

    /*
     * @Description 查询全部课程表，用于穿梭框展示
     * @param
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/5/14 8:51
     */
    @GetMapping("/5")
    public Object findAllCourse(String Csemester){
                      QueryWrapper<Course> queryWrapper=new QueryWrapper<Course>();
                                        queryWrapper.eq("cSemester",Csemester);
    return courseMapper.selectList(queryWrapper);
    }
    /*
     * @Description 查询全部课程表根据班级来查询，在中间表查的，用于展示已拥有的（在穿梭框展示）
     * @param cid
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/5/14 8:51
     */
    @GetMapping("/6")
        public Object findAllClassCourse(Integer studentclassId){
             QueryWrapper<ClassCourse> queryWrapper=new QueryWrapper<ClassCourse>();
             queryWrapper.eq("studentclassid",studentclassId);

    return classCourseMapper.selectList(queryWrapper);
    }
    /*
     * @Description  查询全部学期，要去重
     * @param
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/5/14 9:30
     */
    @GetMapping("/7")
    public Object findAllCourseBySemester(){
        List<Course> courses=courseMapper.selectList(null);
        List<String> slist=new ArrayList<String>();
             for(int i=0;i<courses.size();i++){
                  if(!slist.contains(courses.get(i).getcSemester())){
                      slist.add(courses.get(i).getcSemester());
                  }
             }
     return slist;
    }

@PostMapping
    public Object addClassCourseByCid(ClassCourse cc){


    return  classCourseMapper.insert(cc);
    }
@DeleteMapping
    public Object delClassCourseBycid(ClassCourse cc){

    QueryWrapper<ClassCourse> queryWrapper=new QueryWrapper<>();
    queryWrapper.eq("cid",cc.getCid()).eq("studentclassid",cc.getStudentclassid());
        return classCourseMapper.delete(queryWrapper);
    }

}
